﻿// -------------------------------------------------------------------------
//    @FileName         :    NFDistanceUtil.cpp
//    @Author           :    Gao.Yi
//    @Date             :    2025-09-17
//    @Email			:    445267987@qq.com
//    @Module           :    NFDistanceUtil
//    @Desc             :
//
// -------------------------------------------------------------------------

#include "NFDistanceUtil.h"

#include "proto_common_base.nanopb.h"
#include "proto_common_map.nanopb.h"
#include "NFGameCommon/NFMath.h"
#include "NFLogicCommon/NFCommMacros.h"


enum SamplePolyFlags
{
    SAMPLE_POLYFLAGS_WALK = 0x01, // Ability to walk (ground, grass, road)
    SAMPLE_POLYFLAGS_SWIM = 0x02, // Ability to swim (water).
    SAMPLE_POLYFLAGS_DOOR = 0x04, // Ability to move through doors.
    SAMPLE_POLYFLAGS_JUMP = 0x08, // Ability to jump.
    SAMPLE_POLYFLAGS_DISABLED = 0x10, // Disabled polygon
    SAMPLE_POLYFLAGS_ALL = 0xffff // All abilities.
};

enum
{
    POS_PARAM = 10000,
};

enum
{
    E_Collider_Base_Tolerance = 10000,
    E_Collider_Height_Tolerance = 10000 + E_Collider_Base_Tolerance,
    E_COllider_Base_Square_Tolerance = E_Collider_Base_Tolerance * E_Collider_Base_Tolerance,
};

int NFDistanceUtil::Distance(const Proto_Vector3* pVectorA, const Proto_Vector3* pVectorB)
{
    return NFMath::fast_sqrt_ieee(SquareDistance(pVectorA, pVectorB));
}

int NFDistanceUtil::Distance3D(const Proto_Vector3* pVectorA, const Proto_Vector3* pVectorB)
{
    return NFMath::fast_sqrt_ieee(SquareDistance3D(pVectorA, pVectorB));
}

int64_t NFDistanceUtil::SquareDistance(const Proto_Vector3* pVectorA, const Proto_Vector3* pVectorB)
{
    int64_t llX = pVectorA->x - pVectorB->x;
    int64_t llZ = pVectorA->z - pVectorB->z;

    return llX * llX + llZ * llZ;
}

int64_t NFDistanceUtil::SquareDistance3D(const Proto_Vector3* pVectorA, const Proto_Vector3* pVectorB)
{
    int64_t llX = pVectorA->x - pVectorB->x;
    int64_t llY = pVectorA->y - pVectorB->y;
    int64_t llZ = pVectorA->z - pVectorB->z;

    return llX * llX + llY * llY + llZ * llZ;
}

bool NFDistanceUtil::IsInRange(const Proto_Vector3* pCentre, const Proto_Vector3* pPosition, int iDistance)
{
    int64_t iSqureDistance = (int64_t)iDistance * iDistance;

    return SquareDistance(pCentre, pPosition) <= iSqureDistance;
}

bool NFDistanceUtil::IsInCollider(const Proto_Vector3* pVector, const ColliderDesc* pCollider)
{
    CHECK_EXPR(pVector, false, "");
    CHECK_EXPR(pCollider, false, "");
    int64_t llX = pCollider->location.x - pVector->x;
    int64_t llZ = pCollider->location.z - pVector->z;
    int64_t llY = ::abs(pCollider->location.y - pVector->y);
    int64_t llXTolerance = pCollider->length / 2 + E_Collider_Base_Tolerance;
    int64_t llZTolerance = pCollider->width / 2 + E_Collider_Base_Tolerance;
    int64_t llYTolerance = pCollider->height / 2 + E_Collider_Height_Tolerance;

    return (llX * llX + llZ * llZ) < (llXTolerance * llXTolerance + llZTolerance * llZTolerance) && llY < llYTolerance;
}


